/**
* 用户中心公共js
* @author 邝达峰
*/
var gMember = {
	 //初始化
     init:function()
	 {
		//初始化左边菜单
		this.init_left_menu_action();
        //右边列表初始
         this.searchBar();
         this.checkbox();
	 },
	 //初始化左边菜单动作
	 init_left_menu_action:function()
	 {
		  $(function(){
			  $(".left_menu  span").each(function(){
				  var strId = '',strMenuName = '',strNum = '';
				  strMenuName = 'member_left_menu_span_';
			      strId = $(this).attr('id')
				 if (typeof strId != 'undefined')
				 {
					if(strId.indexOf(strMenuName) > -1)
					{
						 strNum = strId.replace(strMenuName,'')
						 if(typeof($("#"+strId)) == 'object')
						 {
						   $("#"+strId).click(function (){ 
							   gMember.handle_left_menu_click(strNum)
							}
						   );  
						 }
					}
				 }
		      });
		  }); 
	 },
	 //处理左边菜单动作
	 handle_left_menu_click:function(num)
	 {
		 if(num)
		 {
			 var  strMenuName = '',strListName = '';
			 strMenuName = 'member_left_menu_';
			 strListName = 'member_left_list_';
			 if(typeof($("#"+strListName+num)) == 'object')
			 {
				 if($("#"+strListName+num).css('display') == 'block')
				 {
					 $("#"+strListName+num).hide()
					 $("#"+strMenuName+num).addClass('more_img')
					 $("#"+strMenuName+num).height(20)
				 }else
				 {
					 $("#"+strListName+num).show() 
					 $("#"+strMenuName+num).removeClass('more_img')
					 $("#"+strMenuName+num).height('auto')
				 }
			 }
		 }
	 },
	 /**
	 **显示审核内容
	 *@param obj 标签对象或者id名
	 *@param business 处理业务名
	 *@param resid 处理资源id
	 *@param check 审核状态
	 */
	 show_check_reason:function(obj,business,resid,check)
	 {
		 if(business && resid && obj){
			 var busObj = '',strBusiness='',intrResid = 0,checkState=0,color ='';
			 if(typeof obj == 'string'){
				 busObj = $('#'+obj)
			 }else
			 {
				 busObj = obj;
			 }
			 //get data
			 strBusiness =business;
			 checkState =check;
			 intrResid =resid;
			 if(check == 2)
			 {
				 color = '#339933';
			 }else if(check == 1)
			 {
				 color = '#CC3300'
			 }
			 else if(check == 0)
			 {
				 color = '#778899'
			 }

             if($(obj).attr('data-info')){
                var menuObj = ymlayer.tips($(obj).attr('data-info'), busObj, {
                     style: ['background-color:'+color+'; color:#fff',color],
                     maxWidth:185,
                     time: 0
                 });
                 $(obj).on('mouseout',function(){ymlayer.close(menuObj)});
             }else{
                  $.ajaxSetup({async:false});
                 $.getJSON("/sys_check.html",
                     { 'cmd': "get_check_reason", 'checkState':checkState, 'business':strBusiness,'resid':intrResid },
                     function(data){
                         $(obj).attr('data-info', data.info);
                         if(data !=''){
                             if(typeof data =='object'){
                                 var strInfo = '';
                                 strInfo = data.info;
                                 var menuObj = ymlayer.tips(strInfo, busObj, {
                                     style: ['background-color:'+color+'; color:#fff',color],
                                     maxWidth:185,
                                     time: 0
                                 });
                                 $(obj).on('mouseout',function(){ymlayer.close(menuObj)});
                             }//end if

                         }
                     });
                 
             }
		 }
	 },
	 //显示内容提示
	 show_list_tips:function(obj,content,color)
	 {
		  var busObj = '',strInfo='';
		  if(typeof(content) == 'undefined'){
		     return false
		  }
		  if(typeof obj == 'string'){
			  busObj = $('#'+obj)
		   }else
		  {
			  busObj = obj;
		  }
		  if(typeof(color) == 'undefined'){
		     color = '#78BA32';
		  }
		  strInfo = content;
		  var menuObj = ymlayer.tips(strInfo, busObj, {
			  style: ['background-color:'+color+'; color:#fff', color],
			  maxWidth:185,
			  time: 0
		  });
		  $(obj).on('mouseout',function(){ymlayer.close(menuObj)});
	 },
    //列表顶部搜索框绑定键盘enter事件及IE聚焦边框bug
    searchBar: function(){
        $(function(){
            var ua = navigator.userAgent.toLowerCase();
            var IE = (ua.indexOf('msie') > -1 && ua.indexOf('opera') == -1);
            if (IE) {
                $('.sear_blo input')
                    .on('focus', function() {
                        $(this).addClass('ieFocus');
                    }).on('blur', function() {
                        $(this).removeClass('ieFocus');
                    });
            }
            $('.sear_blo input').on('click',function(){
                if(this.value=='请输入关键词')
                    $(this).val('');
            }).on('keydown',function(e) {
                var keynum;
                if (window.event) // IE
                {
                    keynum = e.keyCode
                }
                else if (e.which) // Netscape/Firefox/Opera
                {
                    keynum = e.which
                }
                if (keynum == 13) {
                    gMember.search();
                }
            });
        });
    },
    //列表顶部搜索框搜索页面跳转(审核分类+搜索)
    search: function(){
            var kw = $('.sear_blo input').val(),url='http://'+location.host+'/member/';
			kw=kw.replace(/^\s+|\s+$/g,'');
            if(kw && kw!='请输入关键词'){
                var path = location.pathname.substring(location.pathname.lastIndexOf('/')+1),
                    action,type;
                if(path.indexOf('-') !== -1)
                    action = path.substring(0,path.indexOf('-'));
                else
                    action = path.substring(0,path.indexOf('.'));
                //console.log(action)

                if(path.indexOf('type') !== -1){
                    type = path.substring(path.indexOf('type')+5);
                    if(type.indexOf('-') !== -1)
                        type = type.substring(0,type.indexOf('-'));
                    else
                        type = type.substring(0,type.indexOf('.'));
                }
                if(type)
                    url += action+'-type-'+type+'-kw-'+kw+'.html';
                else
                    url += action+'-kw-'+kw+'.html';

                location.href = url;
            }
            else{
                ymlayer.msg('请输入正确的关键词',1);
			}
    },
    //列表checkbox全选
    checkbox: function(){
        $(function(){
            var checkbox = $('table.input_box :checkbox').not('[disabled]');
            if(checkbox.length > 2){
                var checkbox_all = checkbox.first().add(checkbox.last()),
                    checkbox_item = checkbox.not(checkbox_all);

                checkbox_all.click(function(){
                    checkbox_all.not(this).prop('checked', this.checked);
                    checkbox_item.prop('checked', this.checked);
                })

                checkbox_item.click(function(){
                    if(this.checked){
                        var length = checkbox_item.length;
                        checkbox_item.each(function(i){
                            if(this.checked){
                                if(i== length -1){
                                    checkbox_all.prop('checked', true);
                                }else{
                                    return true;
                                }
                            }else{
                                return false;
                            }
                        });
                    }else{
                        checkbox_all.prop('checked', false);
                    }
                })
            }
            return;
        })
    },
    //获取列表勾选项的id值
    getCheckIds: function(){
        var ids=[],oInput=$('table.input_box input[type=checkbox]');
        for(var i=1; i< oInput.length-1; i++){
            if(oInput[i].checked)ids.push(oInput[i].value);
        }
        return ids;
    },
    /**
     *@param element 输入框对象元素或者对象名称
     *@param param 地址参数名
     **/
    searchKey:function (element,param)
    {
        var path = location.pathname.substring(location.pathname.lastIndexOf('/')+1);
        var pathArr  = '',action ='',objElement='',arrLen = 0,backUrl='',kw='';
        path = path.replace('.html','');
        objElement = $('#kw');
        if(typeof(param) == 'undefined' ){
            param = 'kw';
        }

        if(typeof(element)=='string'){
            objElement = $('#'+element);
        }
        else if(typeof(element)=='object'){
            objElement = element;
        }
        kw = objElement.val();
        kw=kw.replace(/^\s+|\s+$/g,'');
        if(kw=='' || kw=='请输入关键词'){
            ymlayer.msg('请输入正确的关键词',1);
            return false;
        }

        if(path.indexOf('-') !== -1){
            action = path.substring(0,path.indexOf('-'));
        }
        else{
            action = path;
        }
        if(path.indexOf(action+'-') > -1){
            path = path.replace(action+'-','');
            action = action.replace('-','');
        }else{
            path = path.replace(action,'');
        }
        pathArr = path.split('-');
        arrLen = pathArr.length
        if (arrLen > 0){
            for (i = 0; i < arrLen; i++){
                if(pathArr[i] == param){
                   a= pathArr.splice(i,2);
                }
            }
        }
        backUrl = pathArr.join('-');
        if(backUrl){
            backUrl = '-'+backUrl;
        }
        backUrl = 'http://'+location.host+'/admins/'+action+backUrl+'-'+param+'-'+encodeURI(kw)+'.html';
        document.location.href = backUrl;
    },
    //按自定义分类搜索
    searchOfUsort:function(sortid){
        if(sortid){

            var path = location.pathname.substring(location.pathname.lastIndexOf('/')+1);
            var pathArr  = '',action ='',arrLen = 0,backUrl='',param='sortid';
            path = path.replace('.html','');

            if(path.indexOf('-') !== -1){
                action = path.substring(0,path.indexOf('-'));
            }
            else{
                action = path;//corp_demand_purchase

            }

            if(path.indexOf(action+'-') > -1){
                path = path.replace(action+'-','');
                action = action.replace('-','');
            }else{
                path = path.replace(action,'');

            }

            pathArr = path.split('-');

            arrLen = pathArr.length
            if (arrLen > 0){
                for (i = 0; i < arrLen; i++){
                    if(pathArr[i] == param){
                        pathArr.splice(i,2);
                    }
                }
            }
            backUrl = pathArr.join('-');
            if(backUrl){
                backUrl = '-'+backUrl;
            }

            backUrl = 'http://'+location.host+'/member/'+action+backUrl+'-'+param+'-'+sortid+'.html';
            document.location.href = backUrl;
        }
    }

}
//初始化
gMember.init();
